﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BonsaiScrum.Bus;
using BonsaiScrum.Core.Persistance;
using BonsaiScrum.Mvc.Models;
using BonsaiScrum.Core.Command;
using BonsaiScrum.Core.Domain.Shared;
using BonsaiScrum.Core.Domain;
using BonsaiScrum.Mvc.Filters;
using BonsaiScrum.Mvc.Services;

namespace BonsaiScrum.Mvc.Controllers
{
    public class UserController : Controller
    {
        private readonly ICommandBus _commandBus;
        private readonly IMembershipService _membershipService;
        private readonly IFormsAuthenticationService _formsAuthenticationService;
        private readonly IUserReportingRepository _userReportingRepository;

        public UserController() { }
        
        public UserController(ICommandBus commandBus, IMembershipService membershipService, IFormsAuthenticationService formsAuthenticationService, IUserReportingRepository userReportingRepository)
        {
            _commandBus = commandBus;
            _membershipService = membershipService;
            _formsAuthenticationService = formsAuthenticationService;
            _userReportingRepository = userReportingRepository;
        }

        // GET: /User/LogOn
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                if (_membershipService.ValidateUser(model.UserName, model.Password))
                {
                    _formsAuthenticationService.SignIn(model.UserName, model.RememberMe);

                    return RedirectToAction("Index", "Project");
                }
            }

            ModelState.AddModelError("_FORM", "User name or password was not correct.");
            return View();
        }

        public ActionResult LogOff()
        {
            _formsAuthenticationService.SignOut();

            return Redirect("~/");
        }

        [Role(typeof(Manager))]
        public ActionResult Disable()
        {
            var users = _userReportingRepository.GetAllUsers();

            var model = new DisableUserModel
            {
                Users = new SelectList(users, "Id", "UserName")
            };

            return View("Disable", model);
        }

        [Role(typeof(Manager))]
        [HttpPost]
        public ActionResult Disable(DisableUserModel model)
        {
            if (ModelState.IsValid)
            {
                var disableUserCommand = new DisableUser(model.UserId);
                _commandBus.Publish(disableUserCommand);

                return View("UserHasBeenDisabled");
            }
            else
            {
                return RedirectToAction("Disable");
            }

           
        }

        [Role(typeof(Manager))]
        public ActionResult Create()
        {
            var model = new CreateUserModel
            {
                PermissionLevels = new SelectList(Enumeration.GetAll<PermissionLevel>())
            };

            return View("CreateUser", model);
        }

        [Role(typeof(Manager))]
        [HttpPost]
        public ActionResult Create(CreateUserModel model)
        {
            if (ModelState.IsValid)
            {
                var createUserCommand = new CreateNewUser(model.UserName , model.Password , model.FirstName, model.LastName, Enumeration.FromName<PermissionLevel>(model.PermissionLevel));
                _commandBus.Publish(createUserCommand);
                return View("CreateUserSuccess");
            }
            else
            {
                model.PermissionLevels = new SelectList(Enumeration.GetAll<PermissionLevel>());
                return View("CreateUser", model );
            }
        }

        public ActionResult Index()
        {
            var model = new CreateUserModel
            {

                Users = _userReportingRepository.GetAllUsers()
            };
                return View(model);
           
        }
    }
}
